﻿package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Cargo;
import com.edward.service.CargoManager;
import com.edward.service.DT_CargoManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StorageAction  extends ActionSupport{
	

	private static final long serialVersionUID = -8376713977853349771L;

	private Cargo cargo;
	
	private CargoManager cargoManager;
	
	private DT_CargoManager dt_CargoManager;
	
	private int limit;
	
	private int start;
	
	private boolean oversea = false;
	
	private boolean flag = false;

	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}

	public void setCargoManager(CargoManager cargoManager) {
		this.cargoManager = cargoManager;
	}

	public void setDt_CargoManager(DT_CargoManager dtCargoManager) {
		dt_CargoManager = dtCargoManager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
	public boolean isOversea() {
		return oversea;
	}

	public void setOversea(boolean oversea) {
		this.oversea = oversea;
	}

	@Secured("ROLE_DEPOT_ADD")
	public String addStorage() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		cargoManager.addCargo(cargo);
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT")
	public String getStorage() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	  JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		
		if(flag){
			pager = cargoManager.pagerCargo(start, limit,cargo,oversea);
		}else{
			pager = cargoManager.pagerCargo(start, limit,oversea);
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		
		String jsonStr = jsonObject.toString();
		System.out.println(jsonStr);
		response.getWriter().print(jsonStr);
		 
	           
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String updateStorage() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		cargoManager.updateCargo(cargo);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	@Secured("ROLE_DEPOT_UPDATE")
	public String deleteStorage() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		if(dt_CargoManager.checkCargo(cargo.getId())){
		cargoManager.deleteCargo(cargo.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		}else{
		response.getWriter().print("{success : false,msg : '该货物存在单据的引用，不能删除!'}");
		}
		return null;
	}
	
	@Secured("ROLE_DEPOT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	

}
